/**
 * 微信聊天界面
 */
class WxShow extends egret.Sprite {
    /** 加载微信聊天背景 */
    private wx_bg: egret.Bitmap = Main.createBitmapByName("wxBg_png");
    /** 标题显示 */
    private wx_title = new eui.Label();
    /** 时间显示 */
    private timeShow = new eui.Label();
    /** 对话显示容器 */
    private wx_talk_box = new egret.Sprite();
    /** 聊天数据 */
    private nowData;

    public timer;

    private sound = RES.getRes("massage_wav");

    constructor(data) {
        super();

        this.nowData = data;
        console.log(data)
        this.once(egret.Event.ADDED_TO_STAGE, this.init, this);
    }

    private init() {
        this.addChild(this.wx_bg);
        this.addChild(this.wx_title);
        this.addChild(this.timeShow);
        this.addChild(this.wx_talk_box);
        /** 标题显示 */
        this.wx_title.text = this.nowData.name;
        this.wx_title.size = 33;
        this.wx_title.textColor = 0;
        this.wx_title.x = 80;
        this.wx_title.y = 45;
        /** 顶部时间显示 */
        this.timeShow.text = `10:0${3 + Params.MassageNum}`;
        this.timeShow.size = 24;
        this.timeShow.textColor = 0x666666;
        this.timeShow.anchorOffsetX = this.timeShow.width * .5;
        this.timeShow.x = this.stage.stageWidth * .5;
        this.timeShow.y = this.wx_title.y + 50;
        /** 对话容器显示 */
        this.wx_talk_box.width = this.stage.stageWidth;
        this.wx_talk_box.y = this.timeShow.y + this.timeShow.height + 20;

        const arr = this.nowData.talk;
        /** 聊天显示 */

        let num = 0;
        this.timer = new egret.Timer(1000, arr.length);
        this.timer.addEventListener(egret.TimerEvent.TIMER, () => {
            this.sound.play(1, 1);

            let talk;
            if (arr[num].header > 0) {
                talk = new OtherTalk(arr[num].header, arr[num].txt, arr[num].face);
            } else {
                talk = new MeTalk(arr[num].txt, arr[num].face);
            }

            if (arr[num].txt == "逛完在吃点儿好吃的") {
                this.timer.stop();
                egret.Tween.get(this)
                    .wait(1000)
                    .call(this.smallEvent);
            }


            this.wx_talk_box.addChild(talk);
            num++;
        }, this);
        // timer.start();

    }

    /** 小插曲 */
    private smallEvent() {
        /** 插曲容器 */
        const shp = new egret.Sprite();
        shp.graphics.beginFill(0, 1);
        shp.graphics.drawRect(0, 0, this.stage.stageWidth, this.stage.stageHeight);
        shp.graphics.endFill();
        this.stage.addChild(shp);
        /** movieClip */
        const movieClipData = RES.getRes("auuuuuu_json");
        const movieClipTexture = RES.getRes("auuuuuu_png");
        const movieFactory = new egret.MovieClipDataFactory(movieClipData, movieClipTexture);
        const animation = new egret.MovieClip(movieFactory.generateMovieClipData())
        shp.addChild(animation);
        animation.scaleX = 2;
        animation.scaleY = 2;
        animation.frameRate = 6;
        Main.anchorCenter(animation);
        animation.x = this.stage.stageWidth * .5;
        animation.y = this.stage.stageHeight * .5;
        animation.play(1);
        /** 加载尖叫 */
        const auuuuuu = RES.getRes("auuuuuu_mp3");
        auuuuuu.play(1, 1);
        /** 插曲播放完毕 */
        animation.addEventListener(egret.Event.COMPLETE, () => {
            this.stage.removeChild(shp);
            this.timer.start();
        }, this);

    }
}